// Copyright 2012. InMobi. All Rights reserved

package com.inmobi.sharedlock;

import java.util.concurrent.atomic.AtomicLong;

/**
 *
 */
public class ThreadSafeCalculator6 implements AverageCalculator {
  
  private final AtomicLong metricValue = new AtomicLong();
  
  @Override
  public void addValue(long value) {
    metricValue.addAndGet((value << 32) + 1L);
  }

  @Override
  public double getAverage() {
    long value = metricValue.get();
    int count = (int) (value);
    int sum = (int) (value >> 32);
    return count != 0 ? sum*1.0 / count : 0.0;
  }
}
